package io.xxx.omni.oss.custom

import com.google.gson.JsonObject
import io.xxx.omni.oss.core.PageSynchronizer
import io.xxx.omni.oss.sdk.JdClient
import java.time.LocalDateTime

/**
 * 京东
 */
// https://open.jd.com/#/index
abstract class JdSynchronizer : PageSynchronizer<JsonObject>() {

    protected abstract val path: String

    override val pageSize = 500

    override fun getCount(context: Context, response: JsonObject): Int {
        return response["count"].asInt
    }

    override fun getResponse(context: Context, pageNo: Int, containsTotal: Boolean): JsonObject {
        val clientName = "${platform.id}Client${store.id}"
        val client = applicationContext.getBean(clientName, JdClient::class.java)
        val (method, request) = buildRequest(context.startTime, context.endTime, context.parameter, pageNo)
        return client.execute(store, method, request)["data"].asJsonObject
    }

    protected abstract fun buildRequest(
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        parameter: Any?,
        pageNo: Int,
    ): Pair<String, Map<String, Any>>
}